import Vue from 'vue'
import Router from 'vue-router'

const view = (name) => () => import(`@/pages/${name}`)

Vue.use(Router)

const router = new Router({
  mode: 'history',
  scrollBehavior (to, from, savedPosition) {
    return { x: 0, y: 0 }
  },
  routes: [
    {
      path: '/',
      name: 'home',
      component: view('home')
    },
    {
      path: '/accountmanagement',
      name: 'accountManagement',
      component: view('accountManagement'),
      meta: {
        isIntercept: true
      }
    },
    {
      path: '/orderrecords',
      name: 'orderRecords',
      component: view('orderRecords'),
      meta: {
        isIntercept: true
      }
    },
    {
      path: '/paymentinvoice',
      name: 'paymentInvoice',
      component: view('paymentInvoice'),
      meta: {
        isIntercept: true
      }
    },
    {
      path: '/contacts',
      name: 'contacts',
      component: view('contacts'),
      meta: {
        isIntercept: true
      }
    },
    {
      path: '/supplier',
      name: 'supplier',
      component: view('supplier'),
      meta: {
        isIntercept: true
      }
    },
    {
      path: '/importer',
      name: 'importer',
      component: view('importer'),
      meta: {
        isIntercept: true
      }
    },
    {
      path: '/productlibrary',
      name: 'productLibrary',
      component: view('productLibrary'),
      meta: {
        isIntercept: true
      }
    },
    {
      path: '/receivingaddress',
      name: 'receivingAddress',
      component: view('receivingAddress'),
      meta: {
        isIntercept: true
      }
    },
    {
      path: '/fileinformation',
      name: 'fileInformation',
      component: view('fileInformation'),
      meta: {
        isIntercept: true
      }
    },
    {
      path: '/consignor',
      name: 'consignor',
      component: view('consignor'),
      meta: {
        isIntercept: true
      }
    },
    {
      path: '/register',
      name: 'register',
      component: view('register')
    },
    {
      path: '/login',
      name: 'login',
      component: view('login')
    },
    {
      path: '/freightinquiry',
      name: 'freightInquiry',
      component: view('freightInquiry')
    },
    {
      path: '/product',
      name: 'product',
      component: view('product')
    },
    {
      path: '/fcl',
      name: 'fcl',
      component: view('fcl')
    },
    {
      path: '/lcl',
      name: 'lcl',
      component: view('lcl')
    },
    {
      path: '/airtransport',
      name: 'airTransport',
      component: view('airTransport')
    },
    {
      path: '/express',
      name: 'express',
      component: view('express')
    },
    {
      path: '/aboutus',
      name: 'aboutUs',
      component: view('aboutUs')
    }
  ]
})

router.beforeEach((to, form, next) => {
  if (to.meta.isIntercept) {
    if (localStorage.getItem('FOREST_TOKEN')) {
      next()
    } else {
      next()
      // next('/login')
    }
  } else {
    next()
  }
})


export default router
